package com.ruoyi.whc.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.whc.domain.CheckAreaId;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.whc.domain.Checkpoint;
import com.ruoyi.whc.service.ICheckpointService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 检测点管理Controller
 * 
 * @author whc
 * @date 2022-09-22
 */
@RestController
@RequestMapping("/whc/checkpoint")
public class CheckpointController extends BaseController
{
    @Autowired
    private ICheckpointService checkpointService;

    /**
     * 查询检测点管理列表
     */
    @PreAuthorize("@ss.hasPermi('whc:checkpoint:list')")
    @GetMapping("/list")
    public TableDataInfo list(Checkpoint checkpoint)
    {
        startPage();
        List<Checkpoint> list = checkpointService.selectCheckpointList(checkpoint);
        return getDataTable(list);
    }

    /**
     * 导出检测点管理列表
     */
    @PreAuthorize("@ss.hasPermi('whc:checkpoint:export')")
    @Log(title = "检测点管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Checkpoint checkpoint)
    {
        List<Checkpoint> list = checkpointService.selectCheckpointList(checkpoint);
        ExcelUtil<Checkpoint> util = new ExcelUtil<Checkpoint>(Checkpoint.class);
        util.exportExcel(response, list, "检测点管理数据");
    }

    /**
     * 获取检测点管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('whc:checkpoint:query')")
    @GetMapping(value = "/{cpid}")
    public AjaxResult getInfo(@PathVariable("cpid") Long cpid)
    {
        return AjaxResult.success(checkpointService.selectCheckpointByCpid(cpid));
    }

    /**
     * 新增检测点管理
     */
    @PreAuthorize("@ss.hasPermi('whc:checkpoint:add')")
    @Log(title = "检测点管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Checkpoint checkpoint)
    {
        String deptName="";
        String area = checkpoint.getCaddress();
        //去除名字中的空格
        checkpoint.setCpname(checkpoint.getCpname().replace(" ",""));
        //判断级别为区还是县
        if (area.indexOf("区") != -1){
            deptName = splitData(checkpoint.getCaddress(),"市","区"); //拿到区
        } else if (area.indexOf("县") != -1){
            deptName = splitData(checkpoint.getCaddress(),"市","县"); //拿到县
        } else if (area.indexOf("市",area.indexOf("市")+1) != -1){   //出现泉州市南安市
            deptName = splitArea(checkpoint.getCaddress(),"市","市");
        } else{
            return success("-1");
        }
        deptName = deptName.replace(" ","");
        System.out.println("获取到的区或者县 ---"+deptName);
        //判断是否存在该区域是否有合法areaid
        CheckAreaId areaNum = checkpointService.selectCheckPoint(deptName);
        if (areaNum != null){  //存在该区域
            System.out.println("有========================");
            //查询出根据区名的id
            System.out.println("id -------"+areaNum.getAreaid());
            //修改areaid
            checkpoint.setAreaid((long) areaNum.getAreaid());
            //设置状态为停用状态
            checkpoint.setCpstatus("1");
            //插入数据库
            checkpointService.insertCheckpoint(checkpoint);
            return success("0");
        } else {  //不存在该区域,返回错误信息  AreaNum=0
            return success("-1");
        }
    }

    /**
     * 修改检测点管理
     */
    @PreAuthorize("@ss.hasPermi('whc:checkpoint:edit')")
    @Log(title = "检测点管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Checkpoint checkpoint)
    {
        return toAjax(checkpointService.updateCheckpoint(checkpoint));
    }

    /**
     * 删除检测点管理
     */
    @PreAuthorize("@ss.hasPermi('whc:checkpoint:remove')")
    @Log(title = "检测点管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{cpids}")
    public AjaxResult remove(@PathVariable Long[] cpids)
    {
        return toAjax(checkpointService.deleteCheckpointByCpids(cpids));
    }

    public String splitData(String str, String strStart, String strEnd) {
        String tempStr="";
        tempStr = str.substring(str.indexOf(strStart) + 1, str.indexOf(strEnd)+1);
        return tempStr;
    }

    public String splitArea(String str, String strStart, String strEnd) {
        String tempStr="";
        tempStr = str.substring(str.indexOf(strStart) + 1, str.indexOf(strEnd,str.indexOf(strStart) + 1)+1);
        return tempStr;
    }
}
